package com.wbao.dianniu.manager;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.entity.StringEntity;

import android.content.Context;

import com.wbao.dianniu.data.NoticeHandData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.INoticeHandList;
import com.wbao.dianniu.listener.INoticeHandListListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

/**
 * 	
 * 
 * @author yejianjun
 *
 */
public class NoticeHandListManager implements INoticeHandList {

	private INoticeHandListListener mListener;
	private Context mContext;
	
	public NoticeHandListManager(Context context) {
		mContext = context;
	}

	@Override
	public void noticeHandList(int accountId, int page, int rows) {
		// TODO Auto-generated method stub
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.token);
			Command command = new Command();
			command.addAction(Action.NOTICE_HANDLER_LIST)
					.addArgs("accountId", accountId)
					.addArgs("page", page)
					.addArgs("rows", rows);
			batch.addCommand(command);
			stringEntity = new StringEntity(batch.toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		HttpUtil http = new HttpUtil();
		http.req(mContext, stringEntity, new MyResponse() {

			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				List<NoticeHandData> list = JsonUtil.toObjectList(obj, NoticeHandData.class);
				if (null != mListener)
					mListener.onNoticeHandListSuccess(list);
			}

			@Override
			public void responseFailure(String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onNoticeHandListFailure(0, msg);
			}
		});
	}

	@Override
	public boolean addNoticeHandListListener(INoticeHandListListener listener) {
		mListener = listener;
		return mListener == null ? false : true;
	}

	@Override
	public boolean removeNoticeHandListListener(INoticeHandListListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	}

}
